/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package Core;

import java.io.DataInputStream;
import java.io.IOException;
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;

/**
 *
 * @author Александр
 */
public class FGSImage extends Object
{
	private Image top, bottom, left, right;
	private Image [][] corner = new Image[2][2];
	private int rectColor;
	private Image buffer;

	private static int LEFT=0, RIGHT=1, TOP=0, BOTTOM=1;

	public void load(String fgsFile)
	{
		String str = getText(fgsFile);

	    int i0=0, i=0;

		i = str.indexOf(",",i0+1); //Ищем символ "," в строке, начиная с i0+1 буквы
		String c_lt = str.substring(i0,i); //Выделяем часть строки с i0 по i-ый символ

		i0=i;
		i = str.indexOf(",",i0+1);
		String c_rt = str.substring(i0+1,i);

		i0=i;
		i = str.indexOf(",",i0+1);
		String c_lb = str.substring(i0+1,i);

		i0=i;
		i = str.indexOf(",",i0+1);
		String c_rb = str.substring(i0+1,i);

		i0=i;
		i = str.indexOf(",",i0+1);
		String i_t = str.substring(i0+1,i);

		i0=i;
		i = str.indexOf(",",i0+1);
		String i_b = str.substring(i0+1,i);

		i0=i;
		i = str.indexOf(",",i0+1);
		String i_l = str.substring(i0+1,i);

		i0=i;
		i = str.indexOf(",",i0+1);
		String i_r = str.substring(i0+1,i);

		i0=i;
		i = str.indexOf(";",i0+1);
		rectColor = (int)Integer.parseInt(str.substring(i0+1,i), 16);

		try
		{
			left = Image.createImage("/"+i_l);
			right = Image.createImage("/"+i_r);
			top = Image.createImage("/"+i_t);
			bottom = Image.createImage("/"+i_b);
			corner[LEFT][TOP] = Image.createImage("/"+c_lt);
			corner[RIGHT][TOP] = Image.createImage("/"+c_rt);
			corner[LEFT][BOTTOM] = Image.createImage("/"+c_lb);
			corner[RIGHT][BOTTOM] = Image.createImage("/"+c_rb);
		}
		catch (IOException ex)
		{
			ex.printStackTrace();
		}
	}

	public void draw(Graphics screen, int x, int y, int w, int h)
	{
		if(buffer != null && buffer.getWidth() == w && buffer.getHeight() == h)
		{
			screen.drawImage(buffer, x, y, Graphics.LEFT|Graphics.TOP);
			return;
		}

		buffer = Image.createImage(w, h);

		Graphics buf = buffer.getGraphics();

		buf.setColor(rectColor);
		buf.fillRect(0, 0, w, h);
		
		int sx, sy;

		for(sx = corner[LEFT][TOP].getWidth(); sx <= w; sx += top.getWidth())
			buf.drawImage(top, sx, 0, Graphics.LEFT|Graphics.TOP);

		for(sx = corner[LEFT][BOTTOM].getWidth(); sx <= w; sx += bottom.getWidth())
			buf.drawImage(bottom, sx, h, Graphics.LEFT|Graphics.BOTTOM);

		for(sy = corner[LEFT][TOP].getHeight(); sy <= h; sy += left.getHeight())
			buf.drawImage(left, 0, sy, Graphics.LEFT|Graphics.TOP);
		
		for(sy = corner[RIGHT][TOP].getHeight(); sy <= h; sy += right.getHeight())
			buf.drawImage(right, w, sy, Graphics.RIGHT|Graphics.TOP);

		buf.drawImage(corner[LEFT][TOP], 0, 0, Graphics.LEFT|Graphics.TOP);
		buf.drawImage(corner[RIGHT][TOP], w, 0, Graphics.RIGHT|Graphics.TOP);
		buf.drawImage(corner[LEFT][BOTTOM], 0, h, Graphics.LEFT|Graphics.BOTTOM);
		buf.drawImage(corner[RIGHT][BOTTOM], w, h, Graphics.RIGHT|Graphics.BOTTOM);

		screen.drawImage(buffer, x, y, Graphics.LEFT|Graphics.TOP);
	}

	private String getText(String path)
	{
        DataInputStream dis = new DataInputStream(getClass().getResourceAsStream(path));
        StringBuffer strBuff = new StringBuffer();
        int ch = 0;
        try {
            while ((ch = dis.read()) != -1) {
                strBuff.append((char ) ((ch >= 0xc0 && ch <= 0xFF) ? (ch + 0x350) : ch));
            }
            dis.close();
        } catch (Exception e) {
            System.err.println("ERROR in getText() " + e);
        }
        return strBuff.toString();
	}
}
